package com.xingdata.jjxc.ar_navi;

import android.location.Location;
import android.util.Log;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;

import java.nio.ByteBuffer;
import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */

public class AR_Navi {
    static String TAG = "AR_Navi";

    /**
     * 发送导航路径的所有信息到AR
     * @param mStartLatlng 导航起点坐标（当前定位点）
     * @param mAMapNavi  导航对象
     * @return 导航数据包
     */
    public static ByteBuffer sendRouteInfoToAR(NaviLatLng mStartLatlng, AMapNavi mAMapNavi){
        Log.e(TAG, "GuideList size = " + mAMapNavi.getNaviGuideList().size());
        // Log.e(TAG, " getNaviPaths size = " + mAMapNavi.getNaviPaths().size());
        //TODO 所有坐标点集
        List<NaviLatLng> coordLs = mAMapNavi.getNaviPath().getCoordList();
        Log.e(TAG, " getCoordList size = " + coordLs.size());

        short byteCount = 0;//TODO 一个坐标点需要 3*8 = 24 字节，小心溢出！
        int coordlistByteSize = (coordLs.size() + 2)* 3 * 8;// TODO !这样做就不会溢出
        ByteBuffer bb = ByteBuffer.allocate(coordlistByteSize);// TODO 点集大小 缓存区

        bb.put( ByteUtil.getBytes ( mStartLatlng.getLatitude()   )); /// start point
        bb.put( ByteUtil.getBytes ( mStartLatlng.getLongitude()   ));
        bb.put( ByteUtil.getBytes ( (double)0 )); //TODO 因为起点就是原点 起点到原点的距离 0
        byteCount += 3 * 8;
        Log.e(TAG,"======================> 导航坐标点集：");
        for (int i = 0 ; i < coordLs.size() ; i ++){
            float[] distanceToOrigin = new float[1];
            Location.distanceBetween(mStartLatlng.getLatitude(),
                    mStartLatlng.getLongitude(),
                    coordLs.get(i).getLatitude(),
                    coordLs.get(i).getLongitude(),
                    distanceToOrigin);

            //  Log.e("tootzoe" , " i = " + i);
            Log.i(TAG, "坐标|lat,lng,dist[" + i + "]:  " + coordLs.get(i).getLatitude() + "," + coordLs.get(i).getLongitude() + "," + distanceToOrigin[0]+"m");

            //  double testdb = 11.23456789012;
            bb.put( ByteUtil.getBytes ( coordLs.get(i).getLatitude()   ));
            bb.put( ByteUtil.getBytes ( coordLs.get(i).getLongitude()   ));
            bb.put( ByteUtil.getBytes ( (double)distanceToOrigin[0] ));
            byteCount += 3 * 8;

            // Log.e("tootzoe" , "getBytesFromDouble : " + getHex(bb.array()));
        }

        bb.put( ByteUtil.getBytes ( mStartLatlng.getLatitude() ));  /// end point (same as start point)
        bb.put( ByteUtil.getBytes ( mStartLatlng.getLongitude() ));
        bb.put( ByteUtil.getBytes ( (double)0 ));
        byteCount += 3 * 8;

        ByteBuffer packageDataBuffer = ByteBuffer.allocate(coordlistByteSize + 2  + 1);

        byte[] headbyte = new byte[3]; // len(2B)|cmd(1B)|data(...nB...)
        headbyte[2] = 1;

        int len = coordlistByteSize + 1; // 1byte for cmd
        headbyte[0] = (byte) (len & 0xFF );
        headbyte[1] = (byte) ( (len >> 8 ) & 0xFF );


        packageDataBuffer.put(headbyte) ;// len|cmd|data

        packageDataBuffer.put(bb.array() , 0 , byteCount);

        int byteCountForUdp = (byteCount + 2 + 1);

        return packageDataBuffer;// 这个是最后要发送的
    }

    /**
     * 发送导航过程信息到AR
     * @param mStartLatlng 起点
     * @param naviinfo 导航过程信息
     */
    public static ByteBuffer sendNaviInfoToAR(NaviLatLng mStartLatlng, NaviInfo naviinfo){
        Log.e(TAG, " sendToAR() ..... lat: "
                + naviinfo.getCoord().getLatitude()
                + "  lng :"  + naviinfo.getCoord().getLongitude()
                + " iconType =" + naviinfo.getIconType());
        int turnDist = naviinfo.getCurStepRetainDistance(); // TODO 下一个路口的距离信息 ，单位 m

        float[] myResult = new float[1];// TODO 当前车的位置距离原点的长度值

//        NaviLatLng pt1 = new NaviLatLng(31.021871  ,121.441757);
//        NaviLatLng pt2 = new NaviLatLng(31.02301  ,121.445241);

        Location.distanceBetween(mStartLatlng.getLatitude(),
                mStartLatlng.getLongitude(),
                naviinfo.getCoord().getLatitude(),
                naviinfo.getCoord().getLongitude(),
                myResult);
        //  Location.distanceBetween(pt1.getLatitude(),pt1.getLongitude(),pt2.getLatitude(),pt2.getLongitude(),myResult);

        // Log.e("tootzoe" , " dist from start point: " + myResult[0]);

        //  mTxtPosInfo.setText(tmpStr);
        long now = System.currentTimeMillis();// TODO 时间戳
        short byteCount = 0;
        ByteBuffer bb = ByteBuffer.allocate(1024);

        bb.put( ByteUtil.getBytes (naviinfo.getCoord().getLatitude()   )); // TODO 当前坐标信息 double
        bb.put( ByteUtil.getBytes ( naviinfo.getCoord().getLongitude()  ));
        bb.put( ByteUtil.getBytes ( (double)myResult[0] )); //TODO 当前车距离原点信息
        byteCount += 3 * 8; // TODO 3 * double 所有字节,double 类型占8个字节

        byte[] turnInfo = new byte[3];// TODO 车转向信息
        turnInfo[0] =(byte) naviinfo.getIconType();
        turnInfo[1] = (byte) (turnDist & 0xFF );
        turnInfo[2] = (byte) ( (turnDist >> 8 ) & 0xFF );
        bb.put(turnInfo);
        byteCount += 3;

        bb.put(ByteUtil.getBytes(now));
        byteCount += 8;

        ByteBuffer bbOut = ByteBuffer.allocate(byteCount + 2  + 1);

        byte[] headbyte = new byte[3];
        headbyte[2] = 2;

        int len = byteCount + 1;
        headbyte[0] = (byte) (len & 0xFF );
        headbyte[1] = (byte) ( (len >> 8 ) & 0xFF );

        bbOut.put(headbyte) ;
        bbOut.put(   bb.array() , 0 , byteCount);

        int byteCountForUdp = (byteCount + 2 + 1);

        return bbOut;// 这个是最终发送的字节包
    }

    public static  ByteBuffer sendRefPoint(double lat,double lng){
        Log.e(TAG,"======================> Send ref point to AR lat="+lat+",lng="+lng);
        byte[] headbyte = new byte[3];
        int len = 16 + 1;
        headbyte[0] = (byte) (len & 0xFF );
        headbyte[1] = (byte) ( (len >> 8 ) & 0xFF );

        ByteBuffer byteBuffer = ByteBuffer.allocate(16 + 3);
        headbyte[2] = 3;// cmd

        byteBuffer.put(headbyte);

        byteBuffer.put(ByteUtil.getBytes(lat));
        byteBuffer.put(ByteUtil.getBytes(lng));
        return byteBuffer;
    }

    public static  ByteBuffer sendDirection(double direct,double speed){
        Log.e(TAG,"======================> Send direction angle to AR direction="+direct+",speed="+speed);
        byte[] headbyte = new byte[3];
        int len = 8*2 + 1;
        headbyte[0] = (byte) (len & 0xFF );
        headbyte[1] = (byte) ( (len >> 8 ) & 0xFF );

        ByteBuffer byteBuffer = ByteBuffer.allocate(8*2 + 3);
        headbyte[2] = 4;// cmd

        byteBuffer.put(headbyte);

        byteBuffer.put(ByteUtil.getBytes(direct));
        byteBuffer.put(ByteUtil.getBytes(speed));
        return byteBuffer;
    }

    public static  ByteBuffer sendOrientationAngle(double angle){
        Log.e(TAG,"======================> Send OrientationAngle angle to AR angle="+angle);
        byte[] headbyte = new byte[3];
        int len = 8 + 1;
        headbyte[0] = (byte) (len & 0xFF );
        headbyte[1] = (byte) ( (len >> 8 ) & 0xFF );

        ByteBuffer byteBuffer = ByteBuffer.allocate(8 + 3);
        headbyte[2] = 5;// cmd

        byteBuffer.put(headbyte);

        byteBuffer.put(ByteUtil.getBytes(angle));
        return byteBuffer;
    }
}
